<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Users
 *
 * This model represents user authentication data. It operates the following tables:
 * - user account data,
 * - user profiles
 *
 * @package	Sama Manager
 * @author	William M. (http://codejambo.com)
 */
class Mdl_members extends CI_Model
{
	
	function get_members()
	{
		//$this->db->join('fx_profiles','fx_profiles.user_id = users.id');
		$query = $this->db->get('users');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
		
	function public_users()
	{
		$this->db->where('private', 'FALSE');
		$this->db->join('users','users.id = profiles.user_id');
		$query = $this->db->get('profiles');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
function memberships()
	{
		$query = $this->db->get('memberships');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
function get_member_profile($profile)
	{
		$this->db->where('id', $profile);
		$this->db->join('roles','roles.r_id = users.role_id');
		$this->db->join('fx_profiles','fx_profiles.user_id = users.id');
		$query = $this->db->get('users');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
	function member_profile($profile)
	{
		$this->db->where('id', $profile);
		$this->db->where('private', 'FALSE');
		$this->db->join('roles','roles.r_id = users.role_id');
		$this->db->join('fx_profiles','fx_profiles.user_id = users.id');
		$query = $this->db->get('users');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
	function membership_profile($profile)
	{
		$this->db->where('m_id', $profile);
		$query = $this->db->get('memberships');
		if ($query->num_rows() > 0){
			$row = $query->row_array(); 
			return $row;
		} 
	}
	function is_unique_username($username)
	{
		$this->db->where('username', $username);
		$query = $this->db->get('users');
		if ($query->num_rows() > 0){
			return TRUE;
		} else{
			return FALSE;
		}
	}
function user_info($id)
	{
		return $this->db->select()
			->from('users')
			->where('id', $id)
			->get()
			->row()->username;
	}
	function user_role($id)
	{
		return $this->db->select()
			->from('users')
			->where('id', $id)
			->get()
			->row()->role_id;
	}
	function old_avatar($id)
	{
		return $this->db->select()
			->from('fx_profiles')
			->where('user_id', $id)
			->get()
			->row()->avatar;
	}
function login_activities()
	{
		$this->db->join('users','users.id = auth_log.user');
		$query = $this->db->get('auth_log');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
function get_logins($id)
	{
		$this->db->where('user', $id);
		$this->db->join('users','users.id = auth_log.user');
		$query = $this->db->get('auth_log');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
function user_attachments($user)
	{
		$this->db->where('user_to', $user);
		$this->db->or_where('user_from', $user); 
		$query = $this->db->get('messages');
		if ($query->num_rows() > 0){
			return $query->result_array();
		} 
	}
	public function delete_attachment($file_id)
{
	$file = $this->_get_file($file_id);
	if (!$this->db->where('attach_id', $file_id)->delete('attachments'))
	{
		return FALSE;
	}
	unlink('./uploads/attachments/' . $file->file_name);	
	return TRUE;
}
public function _get_file($file_id)
{
	return $this->db->select()
			->from('attachments')
			->where('attach_id', $file_id)
			->get()
			->row();
}
}

/* End of file users.php */
/* Location: ./application/models/auth/users.php */